package com.zg.api.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.api.domain.convert.ApiProjectConvert;
import com.zg.api.domain.req.ApiProjectAddReq;
import com.zg.api.domain.req.ApiProjectEditReq;
import com.zg.api.domain.req.ApiProjectPageReq;
import com.zg.api.domain.vo.ApiProjectVo;
import com.zg.api.service.ApiProjectService;
import com.zg.common.core.R;
import com.zg.common.core.dao.apis.entity.ApiProject;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/16 20:23
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/project")
@Slf4j
@Api(tags = "项目信息")
public class ApiProjectController {

    private final ApiProjectService projectService;

    private final ApiProjectConvert projectConvert;

    @PostMapping("/page")
    public R page(@RequestBody ApiProjectPageReq pageReq) {
        Page<ApiProjectVo> page = projectService.page(pageReq, ApiProjectVo.class);
        return R.success(projectConvert.toEntityPage(page));
    }


    @PostMapping("/add")
    public R add(@Validated @RequestBody ApiProjectAddReq addReq) {
        projectService.add(addReq);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@Validated @RequestBody ApiProjectEditReq editReq) {
        projectService.edit(editReq);
        return R.success();
    }

    @PostMapping("/remove")
    public R remove(@RequestBody  List<Long> ids) {
        projectService.remove(ids);
        return R.success();
    }

    @PostMapping("/bindNamespace")
    public R bindNamespace(@RequestBody  List<Long> ids) {
        projectService.changeNamespace(ids,true);
        return R.success();
    }
    @PostMapping("/unBindNamespace")
    public R unBindNamespace(@RequestBody  List<Long> ids) {
        projectService.changeNamespace(ids,false);
        return R.success();
    }
    @GetMapping("/list")
    public R<List<ApiProjectVo>> list() {
        List<ApiProject> vList=projectService.list();
        return R.success(projectConvert.toVoList(vList));
    }

    @GetMapping("/listEnNamespace")
    public R<List<ApiProjectVo>> listEnNamespace() {
        List<ApiProject> vList=projectService.list().stream().filter(t->t.isEnNamespace()).collect(Collectors.toList());
        return R.success(projectConvert.toVoList(vList));
    }
}
